package 每日一题;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class 超级洗衣机 {
    public int findMinMoves(int[] machines) {
        //衣服总数
        int sum=0;
        sum = Arrays.stream(machines).sum();
        int n=machines.length;
        if (sum%n!=0){
            return -1;
        }
        int num = sum / n;
        for (int i = 0; i < n; i++) {
            machines[i]-=num;
        }
        int res = 0;
        for (int i = 0; i < n-1; i++) {
               res=Math.max(res,Math.abs(machines[i]));
               res=Math.max(res,machines[i+1]);
               machines[i+1]+=machines[i];
        }
        return Math.max(res,Math.abs(machines[n-1]));
    }
}
